package com.example.product;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.example.presentation_libary.FloatTool;
import com.example.presentation_libary.ScreenStartServiceUtils;
import com.example.presentation_libary.interfaces.ScreenStartServiceInterFace;
import com.example.product.presentation.PresentionAddViewUtils;
import com.example.productmodule.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: tjf
 * @date: 2022-10-20 副屏 测试图片和视频的轮播
 * @desc：
 */
public class ProductActivity extends AppCompatActivity implements ScreenStartServiceInterFace {
    private String type;//副屏显示的屏幕布局类型
    private String json;//副屏显示的数据

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(gridLayoutManager);
        List<Data> dataList = new ArrayList<>();

        FileUtils.createOrExistsDir(Constants.DIR_AUXILIARY);
        for (int i = 1; i <= 20; i++) {
            Data data = new Data("" + i, R.mipmap.ic_launcher);
            dataList.add(data);
//            FileUtils.createOrExistsDir(dir_auxiliary+File.separator+"data"+i);
        }
        RecycleViewAdapter recycleViewAdapter = new RecycleViewAdapter(dataList);
        recyclerView.setAdapter(recycleViewAdapter);
        recycleViewAdapter.setOnItemClickListenerr(new RecycleViewAdapter.OnItemClickListenerr() {
            @Override
            public void onGoodsSlotClick(int index, Data data) {
                if (index >= 5) {
                    type = Constants.PagePresentation.PRESENTATION_VIDEO;
                } else {
                    type = Constants.PagePresentation.PRESENTATION_PRODUCT_ADV;
                }
                json = GsonUtils.toJson(data);
                startService();
            }
        });

        FloatTool.RequestOverlayPermission(this);
    }

    /**
     * 启动双屏异显的服务
     * 注册回调时间及启动服务
     */
    private void startService() {
        ScreenStartServiceUtils.getInstance().setServiceInterFace(this);
        ScreenStartServiceUtils.getInstance()
                .startService(this);
    }

    @Override
    public void auxiliaryScreenStartsAndDisplaysSuccess() {
        ScreenStartServiceUtils.getInstance().setCallbackSecondaryScreenTypeAndContent(
                new PresentionAddViewUtils());
        ScreenStartServiceUtils.getInstance().addAuxiliaryScreenTypeAndContent(type, json);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        FloatTool.onActivityResult(requestCode, resultCode, data, this);
    }
}
